package com.yidu.hbatsams.action;
import com.yidu.hbatsams.domain.AtsRivalResult;
import com.yidu.hbatsams.domain.User;
import com.yidu.hbatsams.domain.vo.UserVo;
import com.yidu.hbatsams.service.AtsRivalResultService;
import com.yidu.hbatsams.utils.Message;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


 /*
 * @Author 谭俊
 * @Description //对手成绩
 * @Date 8:40 2019/11/11 0011
 * @Param
 * @return
 **/
@Controller
@RequestMapping("AtsRivalResult")
public class AtsRivalResultController {

    //对手成绩
     @Resource
    AtsRivalResultService atsRivalResultService;


    /**
     * 分页查询对手成绩表
     * @param pageNum
     * @param pageSize
     * @param matchName
     * @return
     */
    @RequestMapping("findAll")
    @ResponseBody
    public Map<String,Object> findAll(@RequestParam("page") Integer pageNum, @RequestParam("limit")Integer pageSize, String matchName, HttpSession session){

        UserVo userVo = (UserVo)session.getAttribute("user");

        if(userVo.getCompanyId().equals("2")||userVo.getCompanyId().equals("5")){


            List<AtsRivalResult> list =  atsRivalResultService.findcoa(pageNum,pageSize,matchName,Integer.valueOf(userVo.getClerkId()));

            int count = atsRivalResultService.countcoa(matchName,Integer.valueOf(userVo.getClerkId()));

            Map<String,Object> map = new HashMap<>();

            map.put("data",list);

            map.put("count",count);

            map.put("code", 0);

            map.put("msg", "");//消息

            return map;

        }

       List<AtsRivalResult> list =  atsRivalResultService.findAll(pageNum,pageSize,matchName);

       int count = atsRivalResultService.count(matchName);

        Map<String,Object> map = new HashMap<>();

        map.put("data",list);

        map.put("count",count);

        map.put("code", 0);

        map.put("msg", "");//消息

        return map;

    }

    /**
     * 删除
     * @param atsRivalResult
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public Message delete(AtsRivalResult atsRivalResult){

        atsRivalResultService.delete(atsRivalResult);

        Message message = new Message();
        message.setNum(1);
        message.setMsg("操作成功");

        return message;

    }

    /**
     * 修改状态
     * @param id
     * @param isva
     * @return
     */
    @RequestMapping("UpdateState")
    @ResponseBody
    public Message UpdateState(Integer id,Integer isva){

        atsRivalResultService.UpdateState(id,isva);

        Message message = new Message();

        message.setNum(1);

        return message;
    }


    /**
     * 增加Or修改
     * @param atsRivalResult
     * @return
     */
    @RequestMapping("AddOrUpdate")
    @ResponseBody
    public Message AddOrUpdate(AtsRivalResult atsRivalResult,HttpSession session) {

        UserVo userVo = (UserVo)session.getAttribute("user");

        Message message = new Message();

        if (atsRivalResult.getResultId() != null) {
            atsRivalResult.setCreater(userVo.getUserName());
            atsRivalResultService.update(atsRivalResult);

        } else {

            System.out.println(userVo.getUserName()+"当前用户");
            atsRivalResult.setCreater(userVo.getUserName());
            atsRivalResultService.insert(atsRivalResult);

        }

        message.setNum(1);

        message.setMsg("操作成功");

        return  message;

    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping("findById")
    @ResponseBody
    public AtsRivalResult findById(Integer id){

        AtsRivalResult atsRivalResult = atsRivalResultService.findById(id);

        System.out.println(atsRivalResult.getMatchTime()+"dfsasdfsa");

        return atsRivalResult;

    }


     /**
      * 根据id查询
      * @param id
      * @return
      */
     @RequestMapping("findByIdriv")
     @ResponseBody
     public AtsRivalResult findByIdriv(Integer id){

         AtsRivalResult atsRivalResult = atsRivalResultService.findByIdriv(id);

         return atsRivalResult;

     }



}
